package com.yusvn.scm.goods.base.controller;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yusvn.scm.common.BaseController;
import com.yusvn.scm.common.kit.MyKit;
import com.yusvn.scm.common.kit.RetKit;
import com.yusvn.scm.goods.base.service.ScmBaseService;

/**
 * SCM所需基础信息(如商品资料，客户资料等)
 * @author liugz
 *
 */
@EnableCORS
public class ScmBaseController extends BaseController {
	
	@Inject
	ScmBaseService srv;
	
	public void index() {
		renderJson();
	}

	//基础数据通用查询 - 单个  (sql模板参数名scmTemplate)
	public void getBase() {
		Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		Record rec = srv.getBase(kv);
		renderJson(RetKit.ok().setData( rec ) );
	}
	
	//基础数据通用查询 - 集合  (sql模板参数名scmTemplate)
	public void getBaseList() {
		Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		List<Record> recList = srv.getBaseList(kv);
		renderJson(RetKit.ok().setData( recList ) );
	}
	
	//基础数据通用查询 - 分页集合  (sql模板参数名scmTemplate)
	public void getBasePage() {
		Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
		if(kv.isEmpty()) {
			kv = getKv();
		}
		// 获取当前登录用户
		kv.set("userid", getLoginUserId());
		
		Page<Record> recPage = srv.getBasePage(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}

	
	//查询条件 - 商品(检索商品名称)
	public void spmc() {
		Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
		if(kv.isEmpty()) {
			kv = getKv();
		}
		kv.set("userCode", getLoginErpUsercode());
		Page<Record> recPage = srv.spmc(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}

	//查询条件 - 商品（确定某个商品是否存在）
	public void getProduct() {
		Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
		if(kv.isEmpty()) {
			kv = getKv();
		}

		Record recPage = srv.getProduct(kv);
		renderJson(RetKit.ok().setData( recPage ) );
	}
	
	//查询条件 - 商品
	public void product() {
		Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Page<Record> recPage = srv.product(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	//查询条件 - 商品颜色
	public void productColor() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Page<Record> recPage = srv.productColor(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	//查询条件 - 商品尺码
	public void productSize() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Page<Record> recPage = srv.productSize(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	//查询条件 - 商品尺码组
	public void productSizeg() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Page<Record> recPage = srv.productSizeg(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	//获取商品属性定义（编号及是否必填、排序）
	public void spsxdy() {
		Kv kv = Kv.create();
		kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		List<Record> recPage = srv.spsxdy(kv);
		renderJson(RetKit.ok().setData( recPage ) );
	}

	//获取商品属性默认值
	public void spsxmrz() {
		Kv kv = Kv.create();
		kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Record rec = srv.spsxmrz(kv);
		renderJson(RetKit.ok().setData( rec ) );
	}
	
	//查询条件 - 商品属性(品牌、年份、季节、大类、中类、小类、)
	//示例1：this.$http.post('/scm/base/productAttr?attr=01', {code: 'aaa', attr: '02'}).then(res => { });
	//示例2：this.$http.post('/scm/base/productAttr?attr=01', {code: 'aaa'}).then(res => { });
	//示例3：this.$http.post('/scm/base/productAttr', {code: 'aaa', attr: '01'}).then(res => { });
	public void productAttr() {
		Kv kv = Kv.create();
		kv.set("attr", getPara("attr")); //如果url上有商品attr属性字段，先取此字段； ploy中也可以传attr参数覆盖
		kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Page<Record> recPage = srv.productAttr(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	//获取商品属性关联关系 （大类-中类-小类）
	public void refAttrib() {
		Kv kv =  MyKit.jsonStrToKv(getRawData(), true); //jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Page<Record> recPage = srv.refAttrib(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	//查询条件 - 客户
	public void agent() {
		Kv kv =  MyKit.jsonStrToKv(getRawData(), true); //jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Page<Record> recPage = srv.agent(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	//查询条件 - 客户类型
	public void khlx() {
		Kv kv =  MyKit.jsonStrToKv(getRawData(), true); //jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		List<Record> recPage = srv.khlx(kv);
		renderJson(RetKit.ok().setData( recPage ) );
	}
	
	//查询条件 - 总部的仓库
	public void zbck() {
		Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Page<Record> recPage = srv.zbck(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	//查询条件 - 总部发货仓库
	public void fhck() {
		Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Page<Record> recPage = srv.fhck(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	//查询条件 - 供应商
	public void supplier() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Page<Record> recPage = srv.supplier(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	  
    //获取版本号
    public void getbbh() {
 
        Record rec = srv.getbbh();
        renderJson(RetKit.ok().setData(rec));
    }
	
	//查询条件 - 内部员工(客服业务员、区域经理、督导、店员、店长、)
	public void personnel() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Page<Record> recPage = srv.personnel(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
	//单个货号信息
	public void getProductById() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Record rec = srv.getProductById(kv);
		renderJson(RetKit.ok().setData( rec ));
	}
	
	//单个客户信息
	public void getAgentById() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		Record rec = srv.getAgentById( kv );
		renderJson(RetKit.ok().setData( rec ));
	}
	
	//查询某个商品有的尺码信息
	public void getProductSizes() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		List<Record> rec = srv.getProductSizes( kv );
		renderJson(RetKit.ok().setData( rec ));
	}
	
	//查询某个商品有的颜色信息
	public void getProductColors() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		List<Record> rec = srv.getProductColors( kv );
		renderJson(RetKit.ok().setData( rec ));
	}
	
	//查询某个商品有的条码信息
	public void getProductSku() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}
		
		List<Record> rec = srv.getProductSku( kv );
		renderJson(RetKit.ok().setData( rec ));
	}

	//查询条件 - 仓店
	public void store() {
		Kv kv = jsonStrToKv(getRawData());
		if(kv.isEmpty()) {
			kv = getKv();
		}

		Page<Record> recPage = srv.store(kv);
		renderJson(RetKit.ok().setData( recPage.getList() ).setCount(recPage.getTotalRow()) );
	}
	
}
